﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml.Linq;

namespace CMTVision.Widgets.Impls
{
    public class WidgetContainer : System.Web.UI.UserControl, CMTVision.Widgets.Common.IWidgetContainer
    {
        #region Ctor

        public WidgetContainer()
            : base()
        {
            IsInEditMode = false;
            
        }

        #endregion

        #region Implement IWidgetContainer interface

        public bool IsInEditMode
        {
            get
            {
                return ViewState["IsInEditMode"] == null ? false : Convert.ToBoolean(ViewState["IsInEditMode"]);
            }
            set
            {
                ViewState["IsInEditMode"] = value;
            }
        }

        public System.Web.UI.Control WidgetHolder
        {
            get { return GetWidgetHolder(); }
        }

        public System.Web.UI.Control DraggableHandle
        {
            get { return GetDraggableHandle(); }
        }



        public Common.IWidget Widget
        {
            get;
            set;
        }

        public virtual void LoadWidget(int templateDataId, string columnid, int index)
        {
            var templateData = (from td in XDocument.Load(Server.MapPath("~/Data/templatedata.xml")).Element("templatedatas").Elements("templatedata")
                                where td.Element("id").Value == templateDataId.ToString()
                                select td
                                 ).FirstOrDefault();
            if (templateData != null)
            {
                var columnData = (from cd in templateData.Element("columns").Elements("column")
                                  where cd.Attribute("id").Value == columnid
                                  select cd).FirstOrDefault();
                if (columnData != null)
                {
                    var widgetData = (from wd in columnData.Element("widgets").Elements("widget")
                                      select wd).ToList().ElementAtOrDefault(index);
                    if (widgetData != null)
                    {
                        LoadWidget(widgetData);
                    }
                }
            }
        }

        //public virtual void LoadWidget(string widgetId)
        //{
        //    var widgetObj = (from wd in XDocument.Load(Server.MapPath("~/Data/widgets.xml")).Element("widgets").Elements("widget")
        //                     where wd.Element("id").Value == widgetId
        //                     select new
        //                     {
        //                         id = wd.Element("id").Value,
        //                         title = wd.Element("title").Value,
        //                         type = wd.Element("type").Value,
        //                         src = wd.Element("src").Value
        //                     }).FirstOrDefault();
        //    var widget = Page.LoadControl(widgetObj.src);
        //    (WidgetHolder as System.Web.UI.IAttributeAccessor).SetAttribute("widgettypeid", widgetObj.id);
        //    (WidgetHolder as System.Web.UI.IAttributeAccessor).SetAttribute("widgetid", widgetObj.id);
        //    WidgetHolder.Controls.Add(widget); 
        //}


        public virtual void ForceRefresh()
        { 
        }

        #endregion

        #region Insistant Methods

        protected virtual void LoadWidget(System.Xml.Linq.XElement widgetData)
        {
            Func<XElement, string, string> getElementValue = (ele, childEleName) => ele.Element(childEleName) != null ? ele.Element(childEleName).Value : null;
            Func<XElement, string, string> getAttributeValue = (ele, attributeName) => ele.Attribute(attributeName) != null ? ele.Attribute(attributeName).Value : null;

            var widgetDataObj = new
            {
                id = getAttributeValue(widgetData, "id"),
                type = getAttributeValue(widgetData, "type"),
                title = getElementValue(widgetData, "title")
            };

            var widgetObj = (from wd in XDocument.Load(Server.MapPath("~/Data/widgets.xml")).Element("widgets").Elements("widget")
                             where wd.Element("id").Value == widgetDataObj.type
                             select new
                             {
                                 id = getElementValue(wd, "id"),
                                 title = getElementValue(wd, "title"),
                                 type = getElementValue(wd, "type"),
                                 src = getElementValue(wd, "src")
                             }).FirstOrDefault();
            if (widgetObj != null)
            {
                var widgetControl = Page.LoadControl(widgetObj.src);
                widgetControl.ID = "Widget" + widgetDataObj.id;
                CMTVision.Widgets.Common.IWidget widget = (widgetControl as CMTVision.Widgets.Common.IWidget);
                widget.WidgetId = widgetDataObj.id;
                widget.WidgetContainer = this;
                widget.IsInEditting = false;
                (DraggableHandle as System.Web.UI.IAttributeAccessor).SetAttribute("widgettypeid", widgetObj.id);
                (DraggableHandle as System.Web.UI.IAttributeAccessor).SetAttribute("widgetid", widgetDataObj.id);
                WidgetHolder.Controls.Add(widgetControl);
                Widget = widget;
                this.ID ="Widget"+ widgetDataObj.id;
            }
        }

        protected virtual System.Web.UI.Control GetWidgetHolder()
        {
            return this;
        }

        protected virtual System.Web.UI.Control GetDraggableHandle()
        {
            return WidgetHolder;
        }

        #endregion


        //public override string UniqueID
        //{
        //    get
        //    {
        //        return "Widget" + Widget.WidgetId;
        //    }
        //}

        //public override string ClientID
        //{
        //    get
        //    {
        //        return "Widget" + Widget.WidgetId;
        //    }
        //}
        public bool SettingsOpen
        {
            get
            {
                object val = ViewState[this.ClientID + "_SettingsOpen"] ?? false;
                return (bool)val;
            }
            set { ViewState[this.ClientID + "_SettingsOpen"] = value; }
        }
        //ctl03_Column2_

        public override System.Web.UI.Control FindControl(string id)
        {
            return base.FindControl(id);
        }
    }
}